package pers.qianyu.month_202101.date_20210118;

import org.junit.*;

/**
 * 6. Z 字形变换
 * https://leetcode-cn.com/problems/zigzag-conversion/
 *
 * @author mizzle rain
 * @date 2021-01-18 12:12
 */
public class Zigzag {
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;
        char[] arr = s.toCharArray();
        int len = arr.length;
        StringBuilder res = new StringBuilder();
        for (int h = 0; h < numRows; h++) {
            if (h == 0 || h == numRows - 1) {
                for (int i = h; i < len; i = 2 * (numRows - 1) + i) {
                    res.append(arr[i]);
                }
            } else {
                for (int i = h, k = 2 * numRows - 2 - h; i < len || k < len;
                     i = 2 * (numRows - 1) + i, k = 2 * (numRows - 1) + k) {
                    if (i < len) res.append(arr[i]);
                    if (k < len) res.append(arr[k]);
                }
            }
        }
        return res.toString();
    }

    @Test
    public void test1() {
        String result = new Zigzag().convert("A", 1);
        System.out.println("result = " + result);
    }
}
